package com.yuandengta.yomall.cart.vo;

import java.math.BigDecimal;
import java.util.List;

/**
 * 购物车 {@link CartVO}
 *
 * @author Kevin
 * @email: 178676392@qq.com
 */
public class CartVO {
    /**
     * 购物项
     */
    private List<CartItemVO> items;
    /**
     * 商品数量
     */
    private Integer countNum;
    /**
     * 商品类型数量
     */
    private Integer countType;
    /**
     * 商品总价
     */
    private BigDecimal totalAmount;
    /**
     * 减免价格
     */
    private BigDecimal reduce;

    public CartVO() {
        reduce = new BigDecimal("0");
    }

    public List<CartItemVO> getItems() {
        return items;
    }

    public void setItems(List<CartItemVO> items) {
        this.items = items;
    }

    /**
     * 计算商品总数量
     * @return
     */
    public Integer getCountNum() {
        int count = 0;
        if (items != null && items.size() > 0) {
            for (CartItemVO item : items) {
                count += item.getCount();
            }
        }
        return count;
    }

    /**
     * 计算商品类型数量
     * @return
     */
    public Integer getCountType() {
        int count = 0;
        if (items != null && items.size() > 0) {
            for (CartItemVO item : items) {
                count += 1;
            }
        }
        return count;
    }

    /**
     * 计算商品总金额
     * @return
     */
    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal("0");
        // 计算购物项总价
        if (items != null && items.size() > 0) {
            for (CartItemVO item : items) {
                // 只有勾选的商品才计算总价
                if (item.getCheck()) {
                    BigDecimal totalPrice = item.getTotalPrice();
                    amount = amount.add(totalPrice);
                }
            }
        }
        // 减去优惠总价
        amount = amount.subtract(getReduce());

        return amount;
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}
